package com.ocean.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间操作类
 *
 * @author zhang
 * @version 1.0
 * @date 2020-7-29
 */
public class TimeUtil {

    private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String TIME_FORMAT_TODAY = "YYYY-MM-dd";

    /**
     * 获取当前整点时间
     *
     * @param flag 补0处理
     * @return 小时
     */
    public static String getTimeHour(boolean... flag) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(flag.length);
        if (flag.length != 0 && flag[0]) {
            return "0" + calendar.get(Calendar.HOUR_OF_DAY);
        }
        return String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
    }

    /**
     * 获取当前整点时间
     *
     * @return 小时
     */
    public static int getTimeHour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取上一个整点时间
     *
     * @return 小时
     */
    public static int getPreviousTimeHour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY) - 1;
    }

    /**
     * 获取昨天日期
     *
     * @return 小时
     */
    public static String getYsterdayTime() {
        Calendar calendar = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT_TODAY);
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当天日期
     *
     * @return 日期字符串
     */
    public static String getTime() {
        Calendar calendar = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT_TODAY);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当天日期
     *
     * @return 日期字符串
     */
    public static String getTime(String format) {
        Calendar calendar = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当天日期，精确到时、分、秒
     *
     * @return 日期字符串
     */
    public static String getTimeOfToDay() {
        Calendar calendar = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 把时间转换为时间戳
     *
     * @return 时间戳
     */
    public static Long conversionTime(String date) throws IllegalArgumentException {
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
        try {
            return dateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        throw new IllegalArgumentException("日期转换出错...");
    }

    /**
     * 将时间戳转为时间
     *
     * @param args 时间戳
     * @return 时间
     */
    public static String stampToDate(Long args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_FORMAT);
        return simpleDateFormat.format(Long.parseLong(String.valueOf(args)));
    }

    /**
     * 将时间转为可阅读的时间
     *
     * @param date date
     * @return 时间
     */
    public static String conversionTime(Date date) {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        return sdf.format(date);
    }

    public static Date conversionDate(String date) throws ParseException {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        return sdf.parse(date);
    }

    /**
     * 判断当前时间距离第二天凌晨的秒数
     *
     * @return 返回值单位为[s:毫秒]
     */
    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis());
    }

    /**
     * 判断当前时间距离第三天凌晨的秒数
     *
     * @return 返回值单位为[s:毫秒]
     */
    public static Long getSecondsSecondEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 2);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis());
    }

    /**
     * 获取两小时后时间
     *
     * @return 时间戳
     */
    public static String oneHoursLaterTime() {
        long currentTime = System.currentTimeMillis();
        System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").format(currentTime));
        currentTime += 1000 * 60 * 60;
        Date date = new Date(currentTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        return dateFormat.format(date);
    }

    /**
     * 判断两个时间差几天
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return 相差天数
     */
    public static int differentDaysByMillisecond(Long beginTime, Long endTime) {
        if (beginTime == null || endTime == null) {
            return 0;
        }
        return (int) ((endTime - beginTime) / (1000 * 3600 * 24));
    }

    /**
     * 获取指定月份之后的日期
     *
     * @param monthLater 几个月
     * @return 时间
     */
    public static LocalDateTime getSpecifyMonth(Integer monthLater) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        return now.plusMonths(monthLater);
    }

    public static LocalDateTime addMonths(LocalDateTime date, int months) {
        return date.plus(Period.ofMonths(months));
    }
}
